package com.cheer.ba.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.footview.CustomerFooter;
import com.andview.refreshview.smileyloadingview.SmileyHeaderView;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.cheer.ba.R;
import com.cheer.ba.constant.CodeStatus;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.Constants;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.OilGoodsInfoModel;
import com.cheer.ba.presenter.impl.FindOilPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.adapter.FindOilAdp;
import com.cheer.ba.view.animation.CustomAnimation;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.XListView.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 搜索柴油批发
 */
public class SearchOilAty extends BaseCommAty {

    //数据加载失败
    @BindView(R.id.id_search_error_data)
    TextView mTxtErrorData;

    @BindView(R.id.id_search_oil_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_search_oil_recyclev)
    RecyclerView mRecycleView;
    private FindOilAdp mFindOilAdp;
    //存储柴油信息
    private List<OilGoodsInfoModel> mOilGoodInfos = new ArrayList<OilGoodsInfoModel>();
    //记录从哪个界面跳转
    private int page_from;
    //搜索条件
    private EditText mEditSearch;
    private String mSaveSearchKey;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_search_oil, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new FindOilPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return !mOilGoodInfos.isEmpty();
    }

    @Override
    public void initView() {
        page_from = getIntent().getIntExtra(IntentKey.General.KEY_PAGE, Constants.DEFAULTVALUE);
        setXRefreshView(mXRefreshView, true, true, false, true, new SmileyHeaderView(aty), new CustomerFooter(aty));
        if (page_from == IntentCode.General.JUMP_FINDOIL) {
            //搜索
            mEditSearch = setSearchTxt("", null);
            setTitleRight("搜索", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //搜索
                    mSaveSearchKey = mEditSearch.getText().toString().trim();
                    if (StringUtils.isEmpty(mSaveSearchKey)) {
                        SVProgressHUD.showInfoWithStatus(aty, "请输入搜索条件");
                    } else {
                        hideSoft(mXRefreshView);
                        if (hasDataInPage()) {
                            mXRefreshView.startRefresh();
                        } else {
                            loadData(!hasDataInPage(), false);
                        }
                    }
                }
            });
        } else if (page_from == IntentCode.General.JUMP_OILMENU) {
            //公司下的柴油
            mSaveSearchKey = getIntent().getStringExtra(IntentKey.General.KEY_DES);
            setTitleText(mSaveSearchKey);
            loadData(!hasDataInPage(), false);
        }
        mRecycleView.setHasFixedSize(true);
        mRecycleView.setLayoutManager(new LinearLayoutManager(aty));
        mFindOilAdp = new FindOilAdp(R.layout.adp_find_oil, mOilGoodInfos, page_from);
        mFindOilAdp.openLoadAnimation(new CustomAnimation());
        mFindOilAdp.setNotDoAnimationCount(3);
        mFindOilAdp.isFirstOnly(false);
        mRecycleView.setAdapter(mFindOilAdp);
    }

    @Override
    public void initData() {
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                loadData(false, true);
            }
        });
        mFindOilAdp.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OilGoodsInfoModel model = (OilGoodsInfoModel) adapter.getItem(position);
                switch (view.getId()) {
                    case R.id.id_oil_company_txt:
                        //进入公司列表
                        Intent intent = new Intent(aty, SearchOilAty.class);
                        intent.putExtra(IntentKey.General.KEY_PAGE, IntentCode.General.JUMP_OILMENU);
                        intent.putExtra(IntentKey.General.KEY_DES, model.orgNickName);
                        showActivity(aty, intent);
                        break;
                    case R.id.id_jump_detail_linear:
                        //进入详情
                        Intent details = new Intent(aty, OilDetailAty.class);
                        details.putExtra(IntentKey.General.KEY_ID, model.uid);
                        showActivity(aty, details);
                        break;
                }
            }
        });
    }

    /**
     * 加载数据
     *
     * @param loadMore 是否加载更多
     */
    public void loadData(boolean isShowLoad, boolean loadMore) {
        ((FindOilPresenter) presenter).findOils(isShowLoad, loadMore, "", "1", "-1", "-1", "-1", "-1", mSaveSearchKey);
        if (!loadMore && !isShowLoad) {
            stopRefresh(mXRefreshView, 4000);
        } else {
            stopLoadMore(mXRefreshView, 4000);
        }
    }


    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_OIL_LIST)) {
                mRecycleView.setVisibility(View.VISIBLE);
                mTxtErrorData.setVisibility(View.GONE);
                //柴油批发信息
                List<OilGoodsInfoModel> mOils = (List<OilGoodsInfoModel>) t;
                if (!loadMore) {
                    mOilGoodInfos.clear();
                    mXRefreshView.stopRefresh();
                } else {
                    mXRefreshView.stopLoadMore();
                }
                if (!mOils.isEmpty()) {
                    mOilGoodInfos.addAll(mOils);
                    mFindOilAdp.notifyDataSetChanged();
                } else {
                    if (loadMore) {
                        mXRefreshView.setLoadComplete(true);
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        //没有数据
                        mRecycleView.setVisibility(View.GONE);
                        mTxtErrorData.setVisibility(View.VISIBLE);
                        mTxtErrorData.setText(getString(R.string.data_is_null));
                    }
                }
            }
        }
    }

    @Override
    public void handlerByPage(int flag1, int flag2, boolean loadMore) {
        mRecycleView.setVisibility(View.GONE);
        mTxtErrorData.setVisibility(View.VISIBLE);
        if (flag2 == CodeStatus.Gegeneral.ERROR_NET) {
            mTxtErrorData.setText(getString(R.string.no_net_work));
        } else if (flag2 == CodeStatus.Gegeneral.ERROR_DATA) {
            mTxtErrorData.setText("数据加载失败，请检查网络是否链接");
        }
    }
}
